require(["core", "bus_permission", "hash_routie", "coAjax"], function(jSouper, bus_permission, hash_routie, coAjax) {

	/*
	 * 获取列表
	 */
	hash_routie.on("permission", function() {
		coAjax.get(appConfig.bus.employees_list, function(result) {
			App.set("$Cache.bus.permission.bus_child_list", result.result);
		});
	});

	/*编辑子账号*/
	//启动编辑
	App.set("$Event.bus.permission.edit_info", function(e, vm) {
		var edit_permission = vm.get();
		//对旧版数据的整理
		var perms_arr = [];
		jSouper.forEach(edit_permission.perms || [], function(key) {
			perms_arr.push.apply(perms_arr, bus_permission.hash_to_keys(key));
		});
		edit_permission.perms = perms_arr;

		App.set("$Cache.bus.edit_permission", edit_permission);
	});
	//移除子账号
	App.set("$Event.bus.permission.remove_employees", function(e, vm) {
		coAjax["delete"](appConfig.bus.remove_employees + vm.get("_id"), function(result) {
			alert("success", result.result);
			hash_routie.emit("permission");
		});
	});
	//新账号
	App.set("$Event.bus.edit_permission.new", function() {
		App.set("$Cache.bus.edit_permission", {});
	});
	// //修改权限
	// App.set("$Event.bus.edit_permission.change_perms", function() {
	// 	var perms_arr = [];
	// 	App.get("$Cache.bus.edit_permission.perms") || [];
	// 	jSouper.forEach(perms_arr, function(key) {
	// 		bus_permission.hash_to_keys(key)
	// 	})
	// 	jSouper.forEach(perms_map, function(checked, key) {
	// 		if (checked) {
	// 			perms_arr.indexOf(key) === -1 && perms_arr.push(key)
	// 		} else {
	// 			var index = perms_arr.indexOf(key);
	// 			if (index !== -1) {
	// 				perms_arr.splice(index, 1)
	// 			}
	// 		}
	// 	});
	// 	console.dir(perms_map);
	// 	console.log(perms_arr);
	// 	App.set("$Cache.bus.edit_permission.perms", perms_arr);
	// });
	//提交编辑
	App.set("$Event.bus.edit_permission.lock", function() {
		var edit_permission = App.get("$Cache.bus.edit_permission");
		/*提交前的数据过滤，删除无用的权限项，可能是过期的数据*/
		var _new_perms = [];
		var _parent_perms_arr = App.get("$Cache.perms");
		jSouper.forEach(edit_permission.perms, function(permission_key) {
			if (_parent_perms_arr.indexOf(permission_key) !== -1) {
				_new_perms.push(permission_key)
			}
		});
		edit_permission.perms = bus_permission.sort(_new_perms);

		if (edit_permission._id) {
			coAjax.put(appConfig.bus.update_employees + edit_permission._id, edit_permission, function(result) {
				alert("success", "修改子账号成功");
				jSouper.forEach(App.get("$Cache.bus.permission.bus_child_list"), function(item, index) {
					if (item._id == edit_permission._id) {
						App.set("$Cache.bus.permission.bus_child_list." + index, result.result);
					}
				});
			});
		} else {
			coAjax.post(appConfig.bus.add_employees, edit_permission, function(result) {
				alert("success", "添加子账号成功");
				App.get("$Event.bus.edit_permission.new")();
				App.push("$Cache.bus.permission.bus_child_list", result.result);
			});
		}
	});
	/*UI交互*/
	App.set("$Event.bus.edit_permission.toggle_checkbox", function(e, vm) {
		var keys = App.get("$Cache.bus.edit_permission.perms") || [];
		var _child_keys = bus_permission.hash_to_keys(vm.get("hash"));
		if (this.checked) {
			keys.push.apply(keys, _child_keys)
		} else {
			jSouper.forEach(_child_keys, function(key) {
				keys = jSouper.$.rm(keys, key)
			});
		}
		App.set("$Cache.bus.edit_permission.perms", keys);
	});
});